home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / PSC Search20387612001.psc / clsRegistry.cls next >
Encoding:
Visual Basic class definition  |  2001-03-22  |  10.9 KB  |  343 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "clsRegistry"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  17. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  18. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  19. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
  20. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
  21. '--------------------------------------------------
  22. Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
  23. Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
  24. '--------------------------------------------------
  25. Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
  26. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
  27.  
  28. Const HKEY_CLASSES_ROOT = &H80000000
  29. Const HKEY_CURRENT_USER = &H80000001
  30. Const HKEY_LOCAL_MACHINE = &H80000002
  31. Const HKEY_USERS = &H80000003
  32. Const HKEY_PERFORMANCE_DATA = &H80000004
  33. Const HKEY_CURRENT_CONFIG = &H80000005
  34. Const HKEY_DYN_DATA = &H80000006
  35. Const REG_SZ = 1                         ' Unicode nul terminated string
  36. Const REG_BINARY = 3                     ' Free form binary
  37. Const REG_DWORD = 4                      ' 32-bit number
  38. Const ERROR_SUCCESS = 0&
  39.  
  40. Enum RegHKey
  41.   Classes_Root = 0
  42.   Current_User = 1
  43.   Local_Machine = 2
  44.   Users = 3
  45.   Current_Config = 4
  46.   Dyn_Data = 5
  47. End Enum
  48.  
  49. Public regKey As RegHKey
  50. Private mKey As Long
  51.  
  52. Private Function GetKey(hKey As RegHKey)
  53.   
  54.   If hKey = Classes_Root Then
  55.     GetKey = HKEY_CLASSES_ROOT
  56.   ElseIf hKey = Current_Config Then
  57.     GetKey = HKEY_CURRENT_CONFIG
  58.   ElseIf hKey = Current_User Then
  59.     GetKey = HKEY_CURRENT_USER
  60.   ElseIf hKey = Dyn_Data Then
  61.     GetKey = HKEY_DYN_DATA
  62.   ElseIf hKey = Local_Machine Then
  63.     GetKey = HKEY_LOCAL_MACHINE
  64.   ElseIf hKey = Users Then
  65.     GetKey = HKEY_USERS
  66.   End If
  67.   
  68. End Function
  69.  
  70. Public Sub CreateKey(hKey As RegHKey, strPath As String)
  71. Dim hCurKey As Long
  72. Dim lRegResult As Long
  73.  
  74. hKey = GetKey(hKey)
  75. lRegResult = RegCreateKey(hKey, strPath, hCurKey)
  76.  
  77. If lRegResult <> ERROR_SUCCESS Then
  78.   ' there is a problem
  79. End If
  80.  
  81. lRegResult = RegCloseKey(hCurKey)
  82.  
  83. End Sub
  84.  
  85. Public Sub DeleteKey(ByVal hKey As RegHKey, ByVal strPath As String)
  86. Dim lRegResult As Long
  87.  
  88. hKey = GetKey(hKey)
  89. lRegResult = RegDeleteKey(hKey, strPath)
  90.  
  91. End Sub
  92.  
  93. Public Sub DeleteValue(ByVal hKey As RegHKey, ByVal strPath As String, ByVal strValue As String)
  94. Dim hCurKey As Long
  95. Dim lRegResult As Long
  96.  
  97. hKey = GetKey(hKey)
  98. lRegResult = RegOpenKey(hKey, strPath, hCurKey)
  99.  
  100. lRegResult = RegDeleteValue(hCurKey, strValue)
  101.  
  102. lRegResult = RegCloseKey(hCurKey)
  103.  
  104. End Sub
  105.  
  106. Public Function GetSettingString(hKey As RegHKey, strPath As String, strValue As String, Optional Default As String) As String
  107. Dim hCurKey As Long
  108. Dim lValueType As Long
  109. Dim strBuffer As String
  110. Dim lDataBufferSize As Long
  111. Dim intZeroPos As Integer
  112. Dim lRegResult As Long
  113.  
  114. ' Set up default value
  115. If Not IsEmpty(Default) Then
  116.   GetSettingString = Default
  117. Else
  118.   GetSettingString = ""
  119. End If
  120.  
  121.  
  122. hKey = GetKey(hKey)
  123.  
  124. ' Open the key and get length of string
  125. lRegResult = RegOpenKey(hKey, strPath, hCurKey)
  126. lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, ByVal 0&, lDataBufferSize)
  127.  
  128. If lRegResult = ERROR_SUCCESS Then
  129.  
  130.   If lValueType = REG_SZ Then
  131.     ' initialise string buffer and retrieve string
  132.     strBuffer = String(lDataBufferSize, " ")
  133.     lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, 0&, ByVal strBuffer, lDataBufferSize)
  134.     
  135.     ' format string
  136.     intZeroPos = InStr(strBuffer, Chr$(0))
  137.     If intZeroPos > 0 Then
  138.       GetSettingString = Left(strBuffer, intZeroPos - 1)
  139.     Else
  140.       GetSettingString = strBuffer
  141.     End If
  142.  
  143.   End If
  144.  
  145. Else
  146.   ' there is a problem
  147. End If
  148.  
  149. lRegResult = RegCloseKey(hCurKey)
  150. End Function
  151.  
  152. Public Sub SaveSettingString(hKey As RegHKey, strPath As String, strValue As String, strData As String)
  153. Dim hCurKey As Long
  154. Dim lRegResult As Long
  155.  
  156. hKey = GetKey(hKey)
  157. lRegResult = RegCreateKey(hKey, strPath, hCurKey)
  158.  
  159. lRegResult = RegSetValueEx(hCurKey, strValue, 0, REG_SZ, ByVal strData, Len(strData))
  160.  
  161. If lRegResult <> ERROR_SUCCESS Then
  162.   'there is a problem
  163. End If
  164.  
  165. lRegResult = RegCloseKey(hCurKey)
  166. End Sub
  167.  
  168. Public Function GetSettingLong(ByVal hKey As RegHKey, ByVal strPath As String, ByVal strValue As String, Optional Default As Long) As Long
  169.  
  170. Dim lRegResult As Long
  171. Dim lValueType As Long
  172. Dim lBuffer As Long
  173. Dim lDataBufferSize As Long
  174. Dim hCurKey As Long
  175.  
  176. hKey = GetKey(hKey)
  177. ' Set up default value
  178. If Not IsEmpty(Default) Then
  179.   GetSettingLong = Default
  180. Else
  181.   GetSettingLong = 0
  182. End If
  183.  
  184. lRegResult = RegOpenKey(hKey, strPath, hCurKey)
  185. lDataBufferSize = 4       ' 4 bytes = 32 bits = long
  186.  
  187. lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, lBuffer, lDataBufferSize)
  188.  
  189. If lRegResult = ERROR_SUCCESS Then
  190.  
  191.   If lValueType = REG_DWORD Then
  192.     GetSettingLong = lBuffer
  193.   End If
  194.  
  195. Else
  196.   'there is a problem
  197. End If
  198.  
  199. lRegResult = RegCloseKey(hCurKey)
  200.  
  201. End Function
  202.  
  203. Public Sub SaveSettingLong(ByVal hKey As RegHKey, ByVal strPath As String, ByVal strValue As String, ByVal lData As Long)
  204. Dim hCurKey As Long
  205. Dim lRegResult As Long
  206.  
  207. hKey = GetKey(hKey)
  208. lRegResult = RegCreateKey(hKey, strPath, hCurKey)
  209.  
  210. lRegResult = RegSetValueEx(hCurKey, strValue, 0&, REG_DWORD, lData, 4)
  211.  
  212. If lRegResult <> ERROR_SUCCESS Then
  213.   'there is a problem
  214. End If
  215.  
  216. lRegResult = RegCloseKey(hCurKey)
  217. End Sub
  218.  
  219. Public Function GetSettingByte(ByVal hKey As RegHKey, ByVal strPath As String, ByVal strValueName As String, Optional Default As Variant) As Variant
  220. Dim lValueType As Long
  221. Dim byBuffer() As Byte
  222. Dim lDataBufferSize As Long
  223. Dim lRegResult As Long
  224. Dim hCurKey As Long
  225.  
  226. hKey = GetKey(hKey)
  227.  
  228. ' setup default value
  229. If Not IsEmpty(Default) Then
  230.   If VarType(Default) = vbArray + vbByte Then
  231.     GetSettingByte = Default
  232.   Else
  233.     GetSettingByte = 0
  234.   End If
  235.  
  236. Else
  237.   GetSettingByte = 0
  238. End If
  239.  
  240. ' Open the key and get number of bytes
  241. lRegResult = RegOpenKey(hKey, strPath, hCurKey)
  242. lRegResult = RegQueryValueEx(hCurKey, strValueName, 0&, lValueType, ByVal 0&, lDataBufferSize)
  243.  
  244. If lRegResult = ERROR_SUCCESS Then
  245.  
  246.   If lValueType = REG_BINARY Then
  247.   
  248.     ' initialise buffers and retrieve value
  249.     ReDim byBuffer(lDataBufferSize - 1) As Byte
  250.     lRegResult = RegQueryValueEx(hCurKey, strValueName, 0&, lValueType, byBuffer(0), lDataBufferSize)
  251.     
  252.     GetSettingByte = byBuffer
  253.  
  254.   End If
  255.  
  256. Else
  257.   'there is a problem
  258. End If
  259.  
  260. lRegResult = RegCloseKey(hCurKey)
  261.  
  262. End Function
  263.  
  264. Public Sub SaveSettingByte(ByVal hKey As RegHKey, ByVal strPath As String, ByVal strValueName As String, byData() As Byte)
  265. ' Make sure that the array starts with element 0 before passing it!
  266. ' (otherwise it will not be saved!)
  267.  
  268. Dim lRegResult As Long
  269. Dim hCurKey As Long
  270.  
  271. hKey = GetKey(hKey)
  272. lRegResult = RegCreateKey(hKey, strPath, hCurKey)
  273.  
  274. ' & = 0
  275. End If
  276.  
  277. ' Open the key and get number of bytes
  278. lReEnd Ifkgn0Hy, ByVasey(hKey, strPath, hCsDataBuffTltrPath,Aues, ByVasey(hKeyrm lDataBufferSize As Long
  279. Dim intZeroPos As Integer
  280. Dim lRegResult As Long hCsDataB
  281.     ' ind get (hKVcte
  282.    tEsMPsraa>0, LeflRegResuSesuSesubByte Then
  283.   ACi b TheR
  284.   ACi b TheR
  285.   ACtYR(strData))g h If VarType(Default) = vbArray + vbByte Then
  286.     Getype(Default) H b teser oE5sma_H b teser oE5sma_H b teser oE5sma_H b teser oE5sma_H b teser oE5sma_H b teser oE5sma_H b teser oEdteser ble TheR
  287.   AC As Long
  288. Dim byBuffer() As Byte
  289. Dim im )im )im 
  290.    l_tesedIma_H b teser oE5W5trPath, hCurKey)f bytes
  291. eeR
  292.   ACi b TheR
  293. i b r osma_H b teser oEre thatPubli = vbArray + vkgn0n = RegCreateKey(hKey, strPe thstrPang
  294.  
  295. hre thatPubli = vbArray + D = vbAeLong
  296.  
  297. hegSetVeTsbACi b TheR
  298.   AiPubli = vbArray + D = (hKVcte
  299.    tEstrVa3reyda2 5ath, hCurKey)f bytesPsValueType, byBuffer(0), lDataBufferSize)
  300.     
  301.     GetSettSiBuffer(yBufferResult At As Long
  302.  
  303. hKey = GetKey(hKey)
  304. lRegResult = RegDeleteytes
  305. eeR
  306.   ACi  e6clt As Long hC RegsultEx(hteytResult = Re1ult yrm fer(0), dn(strData))
  307.  
  308. If lRegResult <> Eytet IfBytctroblemute VB_PreBuffer(yBufferResult At As Long
  309. artolse buffers andalue
  310. lRegResult b TheR
  311.   AiPubli = vbArray + D = (hKVcte
  312.    tEstse buf_RwY
  313. artolse buffers andaSke savet up defrm lDataBufferSize As Long
  314. Dim intZeroPos As Integer
  315. Dim lRegResult As Long hCsDatSsim intZslDt lt As Long hClRegResultKey = Gers andaSke savet As t <> EyGers andaSke N = bb byBuffer
  316. intealu <> EyGers andaSke N = bb byBufferuD vkgn0n = RegCreateKey(hKey, strPe thstrPang
  317.  
  318. hre thatPubli = vbrPe nPFD  ACiLing
  319.  
  320. hre thatPubli = vbrPe nPFD  ACiLing
  321.  
  322. hre thatPublVcte
  323. lRegResulyyData=-CreateKey(hKey, strPe thstrPaesitrm lDataBuffersflRegResuSesuSesBuffeurKrsflRegResuSesufferuD vpResuSesuSesBuffeurKrsflRegResuSesufferuD vpResuSesuSasey(hpthatfferSize)
  324.     
  325.     GetSettingByte = byBuffer
  326.  
  327.  RegResuSethat sheR
  328.   :uSe ThTLreateKey(hKeuerSize)
  329.     
  330.     GetSettingBytt <> Ey  rs
  331. hbE0s"    lslve_E ACiLing
  332. tH  sGteo1ult yrm fer(0),sBuffeurKrsflRegResuSesufferuD vpResuSesuSasey(hpthatfferSize)
  333.     
  334.     GetSettingByte = byBuffer
  335.  
  336.  RegResuSs  GetSettingByk  oE5smaBBufwAs Long hlt yrm <> EyGers andaSke N = bb byBuffer
  337. intealu <>"LreateferTrshKeli = vbArray + D = vbAeLon"wKey, siE4pD = (hKtO"viKey, siE4pD = (hKtO"viKey, siE4pD = (hKtO"viKey, siE4pD = (hKtO"viKey, siE4pD = (hKtO"viKey, siE4pD = (hKtO"viKey, siE4pD = (hKtO"ic Sub rs
  338. Rhatfft_ _ic SuiE4pD = (hKtO"viKey, siECegResult As etSettingByte = khKtOttingByte = khKtT_RwY
  339. artolse buffers andaSke savet up defrm lDataBufferStT_RwY
  340. artolseRrs
  341. Rhatfft_ E5sma_H b teser oE5sma_H b teser oE5sma_H b tese, 4)
  342.  
  343. tingByte = Sy, siEsma_H bDssma_H btingByte = Sy, siEsma_H bDssma_H btivpOptiath, hCurKey)f GetKey(hKetolsmsiIe siESrSsBuffeurKrsf